clear all 
cd "/Users/mariadelmarcanizaresespadafor/Dropbox/Side projects/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Data"
*cd "~/Dropbox/EUI/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Data"

**********************************************
* Table S1 *
**********************************************

use "movilidad_merged_1503.dta", clear
sum distancia_viaje

 
clear

**********************************************

use "gambling_data.dta", clear
drop if school_id==.
merge m:1 apuestas_id using "dapuestas_year.dta"

gen treatment_all=.
replace treatment_all=1 if metres<500 & year>=year_lic
replace treatment_all=0 if metres>500

* This is to manually get data 'wide'

drop num
gen num=.
replace num= n18 if year==2017
replace num= n16 if year==2016
replace num= n15 if year==2015
replace num= n14 if year==2014

drop pau
gen pau=.
replace pau= pau1516 if year==2015
replace pau= pau16 if year==2016
replace pau= pau17 if year==2017
replace pau= pau14 if year==2014

* Income by district: 
drop renta
gen renta = .
replace renta = 31 if distrito==4
replace renta = 40 if distrito==1
replace renta = 51 if distrito==14
replace renta = 53 if distrito==15
replace renta = 60 if distrito==5
replace renta = 34 if distrito==17
replace renta = 48 if distrito==6
replace renta = 48 if distrito==8
replace renta = 56 if distrito==11
replace renta = 30 if distrito==10
replace renta = 27 if distrito==3
replace renta = 25 if distrito==18
replace renta = 24 if distrito==13
replace renta = 34 if distrito==12
replace renta = 36 if distrito==7
replace renta = 47 if distrito==9
replace renta = 26 if distrito==21
replace renta = 30 if distrito==20
replace renta = 31 if distrito==19
replace renta = 34 if distrito==16
replace renta = 46 if distrito==2

* Dummy variable for income: Districts with incomes higher to the median==1
drop renta2
gen renta2 = 0 
replace renta2 = 1 if renta>40

**********************************************
* Table S2 *
**********************************************

sum pau num treatment_all logdist_year publico renta2

**********************************************
* Table S3 *
**********************************************
drop _merge 
drop if school_id == . 
merge 1:1 school_id year using "gambling_data.dta"

tab year, sum(metres_year)
drop metres_year 

** Publico=1 -> charter schools
** Publico=2 -> public schools

** Renta2=0 -> poorer 
** Renta2=1 -> richer 

* Dummy variable for income: Districts with incomes higher to the median==1
drop renta2
gen renta2 = 0 
replace renta2 = 1 if renta>40

drop treatment_all
gen treatment_all=0
** t0
replace treatment_all=1 if metres<500 & year>=year_licence

drop treat
gen treat=treatment_all

*** This makes stata understand data as a panel. And, when applying the xtreg command, adding fixed effects at the observation level. 

xtset school_id year
**********************************************
* Table S4 * This is all in t0
**********************************************

eststo like: xtreg treat publico
eststo like_fe: xtreg treat publico i.year
eststo like_fe_rent: xtreg treat renta publico i.year 

*esttab like like_fe like_fe_rent using "tableS4.tex", keep(publico) replace label se starlevels( * 0.10 ** 0.05 *** 0.010)

**********************************************
* Table S5 *
**********************************************

eststo av0: xtreg pau treat i.year, fe

replace treat=0 if treat!=.
replace treat=1 if metres<500 & year>year_lic

eststo av1: xtreg pau treat i.year, fe

replace treat=0 if treat!=.
replace treat=1 if metres<500 & year+1>=year_lic

eststo av_1: xtreg pau treat i.year, fe

replace treat=0 if treat!=.
replace treat=1 if metres<500 & year+2>=year_lic

eststo av_2: xtreg pau treat i.year, fe

***********************
***** Table S7: t0
***********************

replace treat=0 if treat!=.
replace treat=1 if metres<500 & year>=year_lic

** ATE PAU t0:
eststo av0: xtreg pau treat i.year, fe

*Charter
eststo av_ch0: xtreg pau treat i.year if publico==1, fe

*Public
eststo av_pu0: xtreg pau treat i.year if publico==2, fe

*Charter, poor
eststo av_ch_po0: xtreg pau treat i.year if publico==1 & renta2==0, fe

*Charter, rich
eststo av_ch_ri0: xtreg pau treat i.year if publico==1 & renta2==1, fe

*Public, poor
eststo av_pu_po0: xtreg pau treat i.year if publico==2 & renta2==0, fe

*Public, rich
eststo av_pu_ri0: xtreg pau treat i.year if publico==2 & renta2==1, fe

***********************
***** Table S13: t0
***********************

*** NUM
eststo num0: xtreg num treat i.year, fe

*Charter
eststo num_ch0: xtreg num treat i.year if publico==1, fe

*Public
eststo num_pu0: xtreg num treat i.year if publico==2, fe

*Charter, poor
eststo num_ch_po0: xtreg num treat i.year if publico==1 & renta2==0, fe

*Charter, rich
eststo num_ch_ri0: xtreg num treat i.year if publico==1 & renta2==1, fe

*Public, poor
eststo num_pu_po0: xtreg num treat i.year if publico==2 & renta2==0, fe

*Public, rich
eststo num_pu_ri0: xtreg num treat i.year if publico==2 & renta2==1, fe


***********************
***** Table S6: t+1
***********************

replace treat=0 if treat!=.
replace treat=1 if metres<500 & year>year_lic

eststo av1: xtreg pau treat i.year, fe

*Charter
eststo av_ch1: xtreg pau treat i.year if publico==1, fe

*Public
eststo av_pu1: xtreg pau treat i.year if publico==2, fe

*Charter, poor
eststo av_ch_po1: xtreg pau treat i.year if publico==1 & renta2==0, fe

*Charter, rich
eststo av_ch_ri1: xtreg pau treat i.year if publico==1 & renta2==1, fe

*Public, poor
eststo av_pu_po1: xtreg pau treat i.year if publico==2 & renta2==0, fe

*Public, rich
eststo av_pu_ri1: xtreg pau treat i.year if publico==2 & renta2==1, fe

***********************
***** Table S12: t+1
***********************

eststo num1: xtreg num treat i.year, fe

*Charter
eststo num_ch1: xtreg num treat i.year if publico==1, fe

*Public
eststo num_pu1: xtreg num treat i.year if publico==2, fe

*Charter, poor
eststo num_ch_po1: xtreg num treat i.year if publico==1 & renta2==0, fe

*Charter, rich
eststo num_ch_ri1: xtreg num treat i.year if publico==1 & renta2==1, fe

*Public, poor
eststo num_pu_po1: xtreg num treat i.year if publico==2 & renta2==0, fe

*Public, rich
eststo num_pu_ri1: xtreg num treat i.year if publico==2 & renta2==1, fe

***********************
***** Table S8: t-1
***********************

replace treat=0 if treat!=.
replace treat=1 if metres<500 & year+1>=year_lic

eststo av_1: xtreg pau treat i.year, fe

*Charter
eststo av_ch_1: xtreg pau treat i.year if publico==1, fe

*Public
eststo av_pu_1: xtreg pau treat i.year if publico==2, fe

*Charter, poor
eststo av_ch_po_1: xtreg pau treat i.year if publico==1 & renta2==0, fe

*Charter, rich
eststo av_ch_ri_1: xtreg pau treat i.year if publico==1 & renta2==1, fe

*Public, poor
eststo av_pu_po_1: xtreg pau treat i.year if publico==2 & renta2==0, fe

*Public, rich
eststo av_pu_ri_1: xtreg pau treat i.year if publico==2 & renta2==1, fe

***********************
***** Table S14: t-1
***********************

eststo num_1: xtreg num treat i.year, fe

*Charter
eststo num_ch_1: xtreg num treat i.year if publico==1, fe

*Public
eststo num_pu_1: xtreg num treat i.year if publico==2, fe

*Charter, poor
eststo num_ch_po_1: xtreg num treat i.year if publico==1 & renta2==0, fe

*Charter, rich
eststo num_ch_ri_1: xtreg num treat i.year if publico==1 & renta2==1, fe

*Public, poor
eststo num_pu_po_1: xtreg num treat i.year if publico==2 & renta2==0, fe

*Public, rich
eststo num_pu_ri_1: xtreg num treat i.year if publico==2 & renta2==1, fe


***********************
***** Table S9: t-2
***********************

replace treat=0 if treat!=.
replace treat=1 if metres<500 & year+2>=year_lic

eststo av_2: xtreg pau treat i.year, fe

*Charter
eststo av_ch_2: xtreg pau treat i.year if publico==1, fe

*Public
eststo av_pu_2: xtreg pau treat i.year if publico==2, fe

*Charter, poor
eststo av_ch_po_2: xtreg pau treat i.year if publico==1 & renta2==0, fe

*Charter, rich
eststo av_ch_ri_2: xtreg pau treat i.year if publico==1 & renta2==1, fe

*Public, poor
eststo av_pu_po_2: xtreg pau treat i.year if publico==2 & renta2==0, fe

*Public, rich
eststo av_pu_ri_2: xtreg pau treat i.year if publico==2 & renta2==1, fe



**************************************
* Table S10: Distance
**************************************

eststo dist: xtreg pau logdist_year i.year, fe

*Charter
eststo dist_ch: xtreg pau logdist_year i.year if publico==1, fe

*Public
eststo dist_pu: xtreg pau logdist_year i.year if publico==2, fe

*Charter, poor
eststo dist_ch_po: xtreg pau logdist_year i.year if publico==1 & renta2==0, fe

*Charter, rich
eststo dist_ch_ri: xtreg pau logdist_year i.year if publico==1 & renta2==1, fe

*Public, poor
eststo dist_pu_po: xtreg pau logdist_year i.year if publico==2 & renta2==0, fe

*Public, rich
eststo dist_pu_ri: xtreg pau logdist_year i.year if publico==2 & renta2==1, fe


*****************************************
****** Table S19: Proximity
*****************************************

replace treat=0 if treat!=.
replace treat=1 if metres<500 & metres>200 & year>year_lic & publico==2

gen t200=0 if treat!=.
replace t200=1 if metres<=200 & year>year_lic & publico==2

eststo prox: xtreg pau c.treat c.t200 i.year, fe

**************************************
* main results: educational perf 
**************************************

*cd "/Users/mariadelmarcanizaresespadafor/Dropbox/Side projects/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Tables"

*cd "~/Dropbox/EUI/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Tables"

* TABLE S5 *  
*esttab av_2 av_1 av0 av1 using "app_average_.tex", keep(treat) replace label se starlevels( * 0.10 ** 0.05 *** 0.010)

* Table S6 *
*esttab av_2 av_pu_2 av_pu_po_2 av_pu_ri_2 av_ch_2 av_ch_po_2 av_ch_ri_2 using "app_t_2_.tex", keep(treat) replace label se starlevels( * 0.10 ** 0.05 *** 0.010)

* Table S7 *
*esttab av_1 av_pu_1 av_pu_po_1 av_pu_ri_1 av_ch_1 av_ch_po_1 av_ch_ri_1 using "app_t_1_.tex", keep(treat) replace label se starlevels( * 0.10 ** 0.05 *** 0.010)

* Table S8 *
*esttab av0 av_pu0 av_pu_po0 av_pu_ri0 av_ch0 av_ch_po0 av_ch_ri0 using "app_t0_.tex", keep(treat) replace label se starlevels( * 0.10 ** 0.05 *** 0.010)

* Table S9 *
*esttab av1 av_pu1 av_pu_po1 av_pu_ri1 av_ch1 av_ch_po1 av_ch_ri1 using "app_t1_.tex", keep(treat) replace label se starlevels( * 0.10 ** 0.05 *** 0.010)

**************************************
* num students 
**************************************

* TABLE S11 *  
*esttab num_1 num0 num1 using "num_average_.tex", keep(treat) replace label se starlevels( * 0.10 ** 0.05 *** 0.010)

* TABLE S12 *  
*esttab num_1 num_pu_1 num_pu_po_1 num_pu_ri_1 num_ch_1 num_ch_po_1 num_ch_ri_1 using "num_t_1_.tex", keep(treat) replace label se starlevels( * 0.10 ** 0.05 *** 0.010)

* TABLE S13 *  
*esttab num0 num_pu0 num_pu_po0 num_pu_ri0 num_ch0 num_ch_po0 num_ch_ri0 using "num_t0_.tex", keep(treat) replace label se starlevels( * 0.10 ** 0.05 *** 0.010)

* TABLE S14 *  
*esttab num1 num_pu1 num_pu_po1 num_pu_ri1 num_ch1 num_ch_po1 num_ch_ri1 using "num_t1_.tex", keep(treat) replace label se starlevels( * 0.10 ** 0.05 *** 0.010)

**************************************
* Table S10: Distance
***************************************
*esttab dist dist_pu dist_pu_po dist_pu_ri dist_ch dist_ch_po dist_ch_ri using "distance_tab_.tex", keep(logdist_year) replace label se starlevels( * 0.10 ** 0.05 *** 0.010)

*****************************************
****** Table S19: Proximity
*****************************************
*esttab prox using "intensity_tab_.tex", keep(logdist_year) replace label se starlevels( * 0.10 ** 0.05 *** 0.010)

*****************************************


*****************************************
****** Rental Prices
*****************************************

cd"/Users/mariadelmarcanizaresespadafor/Dropbox/Side projects/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Estimates"

*cd "~/Dropbox/EUI/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Estimates"

drop treatment_all

gen treatment_all=0
replace treatment_all=1 if metres<500 & year>=year_lic

drop treat
gen treat = treatment_all 

gen inter = treat*publico

xtreg pau treat publico inter i.year, fe
regsave inter using "diff_type_S15.dta", tstat pval ci level(90) replace addlabel (Income, All, inc, 1, School, All, Level, 90)

xtreg pau treat publico inter i.year, fe
regsave inter using "diff_type_S15.dta", tstat pval ci level(95) append addlabel (Income, All, inc, 1, School, All, Level, 95)


xtreg pau treat publico inter i.year if renta2==0, fe
regsave inter using "diff_type_S15.dta", tstat pval ci level(90) append addlabel (Income, Poor, inc, 0, School, All, Level, 90)

xtreg pau treat publico inter i.year if renta2==0, fe
regsave inter using "diff_type_S15.dta", tstat pval ci level(95) append addlabel (Income, Poor, inc, 0, School, All, Level, 95)

use "diff_type_S15.dta", clear
gen ci_upper90 = ci_upper if Level==90
gen ci_lower90 = ci_lower if Level==90
gen ci_upper95 = ci_upper if Level==95
gen ci_lower95 = ci_lower if Level==95 
save "diff_type_S15.dta", replace



clear all

* Setting directory*
cd"/Users/mariadelmarcanizaresespadafor/Dropbox/Side projects/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Data"
*cd "~/Dropbox/EUI/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Data"

use "gambling_data.dta", clear

*drop _merge
merge m:1 apuestas_id using "dapuestas_year.dta"

gen treatment_all=0
replace treatment_all=1 if metres<500 & year>year_lic

* Prices m2 by district is variable renta
gen Price=alquiler
label variable Price "Rent prices in euros/m2"

* Income by district: 
drop renta
gen renta = .
replace renta = 31 if distrito==4
replace renta = 40 if distrito==1
replace renta = 51 if distrito==14
replace renta = 53 if distrito==15
replace renta = 60 if distrito==5
replace renta = 34 if distrito==17
replace renta = 48 if distrito==6
replace renta = 48 if distrito==8
replace renta = 56 if distrito==11
replace renta = 30 if distrito==10
replace renta = 27 if distrito==3
replace renta = 25 if distrito==18
replace renta = 24 if distrito==13
replace renta = 34 if distrito==12
replace renta = 36 if distrito==7
replace renta = 47 if distrito==9
replace renta = 26 if distrito==21
replace renta = 30 if distrito==20
replace renta = 31 if distrito==19
replace renta = 34 if distrito==16
replace renta = 46 if distrito==2

* Dummy variable for income: Districts with incomes higher to the median==1
drop renta2
gen renta2 = 0 
replace renta2 = 1 if renta>40


drop treat
gen treat=treatment_all

xtset school_id year

***********************
***********************
*cd"/Users/mariadelmarcanizaresespadafor/Dropbox/Side projects/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Tables"

*cd "~/Dropbox/EUI/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Tables"

**********************************************
* Table S15: t+1
**********************************************

* Avg
eststo price1: xtreg Price treat i.year, fe

* Public
eststo price2: xtreg Price treat i.year if publico==2, fe

* Charter
eststo price3: xtreg Price treat i.year if publico==1, fe

* Public, poor
eststo price4: xtreg Price treat i.year if publico==2 & renta2==0, fe

* Public, rich
eststo price5: xtreg Price treat i.year if publico==2 & renta2==1, fe

* Charter, poor
eststo price6: xtreg Price treat i.year if publico==1 & renta2==0, fe

* Charter, rich
eststo price7: xtreg Price treat i.year if publico==1 & renta2==1, fe

**********************************************
* Table S16: t0
**********************************************

drop treatment_all 

gen treatment_all=0
replace treatment_all=1 if metres<500 & year>=year_lic

drop treat
gen treat=treatment_all

* Avg
eststo price1_0: xtreg Price treat i.year, fe

* Public
eststo price2_0: xtreg Price treat i.year if publico==2, fe

* Charter
eststo price3_0: xtreg Price treat i.year if publico==1, fe

* Public, poor
eststo price4_0: xtreg Price treat i.year if publico==2 & renta2==0, fe

* Public, rich
eststo price5_0: xtreg Price treat i.year if publico==2 & renta2==1, fe

* Charter, poor
eststo price6_0: xtreg Price treat i.year if publico==1 & renta2==0, fe

* Charter, rich
eststo price7_0: xtreg Price treat i.year if publico==1 & renta2==1, fe

**********************************************
* Table S17: t-1
**********************************************

drop treatment_all 

gen treatment_all=0
replace treatment_all=1 if metres<500 & year+1>=year_lic

drop treat
gen treat=treatment_all

eststo price1_1: xtreg Price treat i.year, fe

eststo price2_1: xtreg Price treat i.year if publico==2, fe

eststo price3_1: xtreg Price treat i.year if publico==1, fe

eststo price4_1: xtreg Price treat i.year if publico==2 & renta2==0, fe

eststo price5_1: xtreg Price treat i.year if publico==2 & renta2==1, fe

eststo price6_1: xtreg Price treat i.year if publico==1 & renta2==0, fe

eststo price7_1: xtreg Price treat i.year if publico==1 & renta2==1, fe

* TABLE S15 * 
*esttab price1 price2 price4 price5 price3 price6 price7 using "price_tab1_.tex", keep(treat) replace label se starlevels( * 0.10 ** 0.05 *** 0.010)

* TABLE S16 * 
*esttab price1_0 price2_0 price4_0 price5_0 price3_0 price6_0 price7_0 using "price_tab0_.tex", keep(treat) replace label se starlevels( * 0.10 ** 0.05 *** 0.010)

* TABLE S17 * 
*esttab price1_1 price2_1 price4_1 price5_1 price3_1 price6_1 price7_1 using "price_tab_1_.tex", keep(treat) replace label se starlevels( * 0.10 ** 0.05 *** 0.010)

*****************************************
****** Table S18: placebo, starbucks
*****************************************

cd"/Users/mariadelmarcanizaresespadafor/Dropbox/Side projects/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Data"

*cd"~/Dropbox/EUI/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Data"

use gambling_data.dta, clear

xtset school_id year

*********************

*cd"/Users/mariadelmarcanizaresespadafor/Dropbox/Side projects/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Tables"
*cd"~/Dropbox/EUI/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Tables"


eststo star1: xtreg pau treat i.year, fe

eststo star2:  xtreg pau treat i.year if renta2==0, fe

eststo star3: xtreg pau treat i.year if renta2==1, fe

eststo star4: xtreg pau logdist_year_star i.year, fe

eststo star5: xtreg pau logdist_year_star i.year if renta2==0, fe

eststo star6: xtreg pau logdist_year_star i.year if renta2==1, fe

* TABLE S18 * 
*esttab star1 star2 star3 star4 star5 star6 using "star_tab_.tex", keep(treat logdist_year_star) replace label se starlevels( * 0.10 ** 0.05 *** 0.010)


